package com.c1202m.dao;

import com.c1202m.gui.ATMSystem;
import com.c1202m.hibernate.entities.QuanTri;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class DangNhapDAO {

    public static SessionFactory sessionFactory;
    public static QuanTri quanTri = null;
    public static ATMSystem as = null;

    public DangNhapDAO(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public QuanTri kiemTraDangNhap(String email, String matKhau) {
        Session session = sessionFactory.openSession();
        QuanTri quanTri = null;
        try {
            session.getTransaction().begin();
            Query query = session.createQuery("from QuanTri where email = ? and matKhau = ? and trangThai = ?");
            query.setString(0, email);
            query.setString(1, matKhau);
            query.setString(2, "TRUE");
            List list = query.list();
            if (!list.isEmpty()) {
                quanTri = (QuanTri) list.get(0);
            }
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return quanTri;
    }
}
